package do

import (
	"fmt"
	"time"

	"gorm.io/gorm"
)

// 超市促销员
type SalesPerson struct {
	ID int `gorm:"primarykey"`

	SupermarketID int `gorm:"column:supermarket_id;NOT NULL;index"`

	// 姓名
	Name string `gorm:"column:name;NOT NULL;unique"`
	// 备注
	Note string

	// 底薪
	BasicSalary Money `gorm:"column:salary;NOT NULL"`

	CreatedAt time.Time `gorm:"autoCreateTime;type:datetime(0)"`
	UpdatedAt time.Time `gorm:"autoUpdateTime;type:datetime(0)"`
}

func (s *SalesPerson) BeforeSave(tx *gorm.DB) (err error) {
	if s.Name == "" {
		return fmt.Errorf("促销员姓名不能为空")
	}

	if s.SupermarketID <= 0 {
		return fmt.Errorf("超市id非法")
	}

	return nil
}

func (SalesPerson) TableName() string {
	return "sales_person"
}
